<?php defined('BASEPATH') or exit('No direct script access allowed');

class Ranking_mproc extends MY_Model {
	//public static  $i;
	public $connection_msg;
	
	function Ranking_mproc(){
		//$this->i= 0; 
		$this->connection_msg = array('valid'=>0, 'not_valid'=>0);
	}
	
	function resetConnectCountMsg(){
		$this->connection_msg = array('valid'=>0, 'not_valid'=>0);
	}
	
	function check_and_process_user_posted( $connection_id, $user_id, $connection_id_reserve ){
		//check
		$r= $this->db->query("SELECT * FROM rs_post WHERE connection_id=$connection_id AND user_id=$user_id")->result();
		if(empty($r)){ //insert
			$this->db->insert('rs_post', array('connection_id'=>$connection_id, 'user_id'=>$user_id, 'posted_time'=>time(),'is_connected'=>1 ) );
			if($connection_id_reserve != 0){
				$this->db->where(array('connection_id'=>$connection_id_reserve,'user_id'=>$user_id))->update('rs_post',array('is_connected'=> 0) ) ;	
			}
			$this->connection_msg['valid']++;
		}else{
			$this->connection_msg['not_valid']++;
		}
		
		//check
		$r2= $this->db->query("SELECT * FROM rs_temp_post WHERE connection_id=$connection_id AND user_id=$user_id")->result();
		if(empty($r)){ //insert
			$this->db->insert('rs_temp_post', array('connection_id'=>$connection_id, 'user_id'=>$user_id, 'posted_time'=>time() ) );
		}
	
		//update for your ranking
		$this->update_user_ranking($connection_id, $user_id);
		//return true;
	}
	
	function update_user_ranking($connection_id, $user_id){
		/*	$u_rc = $this->db->where('id',$user_id)->get('ff_users_fb')->result();
			$conn_id_arr = array();
			
			$mix_arr = explode(';', $u_rc[0]->user_ranking );
			foreach($mix_arr as $e){
				$tmp = explode(',', $e);
				$conn_id_arr[] = $tmp[0];
			}
			if(empty($conn_id_arr)){
				$push_e = "$connection_id,0";
				array_push($mix_arr, $push_e);
			}else{
				if(!in_array($connection_id, $conn_id_arr)){
					$push_e = "$connection_id,0";
					array_push($mix_arr, $push_e);
				}
			}
			$mix_arr = array_filter($mix_arr);
			$user_ranking = implode(';', $mix_arr); 
			$this->db->where('id',$user_id)->update('ff_users_fb',array('user_ranking'=>$user_ranking));
			//return;
		*/	
	}
	
	
	function update_connection($high, $low, $user_id){
		// check if existed in database upper_content_id:== $high AND lower_content_id:== $low
		$result_obj_AB= $this->db->query("SELECT * FROM `rs_connection` WHERE rs_connection.upper_content_id= $high AND rs_connection.lower_content_id= $low ")->result();
		
		// check if existed in database upper_content_id:== $low AND lower_content_id:== $high
		$result_obj_BA= $this->db->query("SELECT * FROM `rs_connection` WHERE rs_connection.upper_content_id= $low AND rs_connection.lower_content_id= $high ")->result();
		
		if(!empty( $result_obj_BA )){
			$id_BA = $result_obj_BA[0]->id;
		}else{
			$id_BA = 0;
		}
		 
		
		if(!empty($result_obj_AB)){
			$this->db->where( 'id', $result_obj_AB[0]->id )->update('rs_connection', array('last_posted_time'=> time()));	
			$this->check_and_process_user_posted($result_obj_AB[0]->id, $user_id, $id_BA);
		}
		
		$time = time();
		$this->db->where('id',$high)->update('ff_rs_content',array('modified_time'=>$time));
		$this->db->where('id',$low)->update('ff_rs_content',array('modified_time'=>$time));
		//
		if( !empty($result_obj_AB) AND $result_obj_AB[0]->is_connected == 1 ){
		//	$this->db->where( 'id', $result_obj_AB[0]->id )->update('rs_connection', array('weight'=> $result_obj_AB[0]->weight + 1));
			$this->db->where( 'id', $result_obj_AB[0]->id )->update('rs_connection', array('last_posted_time'=> time()));
			
		//	$this->check_and_process_user_posted($result_obj_AB[0]->id, $user_id);
		
			return $this->connection_msg;
		}
		elseif( !empty($result_obj_AB) AND $result_obj_AB[0]->is_connected == 0 AND !empty($result_obj_BA) AND $result_obj_BA[0]->is_connected == 1){
			if( $result_obj_AB[0]->weight + 1 < $result_obj_BA[0]->weight ){
			//	$this->db->where( 'id', $result_obj_AB[0]->id )->update('rs_connection', array('weight'=> $result_obj_AB[0]->weight + 1));
				$this->db->where( 'id', $result_obj_AB[0]->id )->update('rs_connection', array('last_posted_time'=> time()));
				
			//	$this->check_and_process_user_posted($result_obj_AB[0]->id, $user_id);
			
				return $this->connection_msg;
			}
		}// else if empty A> B : insert to rs_connection and rs_temp_post and rs_post
		elseif( empty($result_obj_AB) ){
			$this->db->insert('rs_connection', array('upper_content_id'=> $high, 
													 'lower_content_id'=> $low,
													 'last_posted_time'=> time(),
													 'weight'=> 0 ,
													 'is_connected'=> 0 
													)
							 );
			$temp_post_connection_id= $this->db->insert_id();
			
			$this->check_and_process_user_posted($temp_post_connection_id, $user_id, $id_BA);
			return $this->connection_msg;
		}else{
			return $this->connection_msg;
		}
	/*
	// check if existed in database upper_content_id:== $high AND lower_content_id:== $low
		$result_obj= $this->db->query("SELECT * FROM `rs_connection` INNER JOIN rs_post ON rs_connection.id=rs_post.connection_id WHERE rs_connection.upper_content_id= $high AND rs_connection.lower_content_id= $low AND rs_post.user_id=$user_id")->result();
		
		//if not exist
		if(empty($result_obj)){
			$this->db->insert('rs_connection', array('upper_content_id'=> $high, 
													 'lower_content_id'=> $low,
													 'last_posted_time'=> time(),
													 'weight'=> 1 ,
													 'is_connected'=> 0 
													)
							 );
			$temp_post_connection_id= $this->db->insert_id();				 
		}
		else{
			//check if user had inserted 
			$users_arr = array();
			$r= $this->db->where('connection_id',$result_obj[0]->id)->get('rs_post')->result();
			foreach($r as $o){
				$users_arr[] = $one->user_id;
			}
		//	$users_arr= explode(',', $result_obj[0]->posted_by);
			//if true
			if(in_array($user_id, $users_arr)){
				//update last_posted_time
				$this->db->where('id', $result_obj[0]->id)->update('rs_connection', array('last_posted_time'=>time()));
			} 
			else{
				//push user
			//	array_push($users_arr, $user_id);
			//	$this->db->where('id', $result_obj[0]->id)->update('rs_connection', array('last_posted_time'=>time(), 'posted_by'=>implode(',', $users_arr)));
				$this->db->insert('rs_post', array('connection_id'=>$result_obj[0]->id, 'user_id'=>$user_id, 'posted_time'=>time() ) );
			}
			
			$temp_post_connection_id= $result_obj[0]->id;
		}
		
		// Now insert connection_id into `rs_temp_post`
		$res= $this->db->query("SELECT * FROM rs_temp_post WHERE connection_id= $temp_post_connection_id AND user_id= $user_id")->result();
		if(empty($res)){
			$this->db->insert('`rs_temp_post`',array(
									'connection_id'=> $temp_post_connection_id,
									'user_id'=> $user_id,
									'posted_time'=> time()
								)

			);
		}
		else{
			$this->db->query("UPDATE rs_temp_post SET posted_time=".time()." WHERE connection_id= $temp_post_connection_id AND user_id= $user_id");
		}
		return true;	
	
			
		//check if 		
		$is_reverse = $this->db->query("SELECT rs_connection.*,rs_post.* FROM rs_connection INNER JOIN rs_post ON rs_connection.id=rs_post.connection_id
		 WHERE lower_content_id=$high AND upper_content_id=$low AND user_id=$user_id")->result();

		if(!empty($is_reverse)){
			//delete at rs_post
			$this->db->where(array('connection_id'=>$is_reverse[0]->connection_id,'user_id'=>$user_id))->delete('rs_post');
			//decrease -1 at rs_connection
			$this->db->where('id',$is_reverse[0]->id)->update('rs_connection',array('weight'=>$is_reverse[0]->weight -1));
		}
		
		$is_existed = $this->db->query("SELECT rs_connection.* FROM rs_connection
		 WHERE lower_content_id=$low AND upper_content_id=$high")->result();
		
		if(!empty($is_existed)){
			//check rs_post
			$r = $this->db->where(array('connection_id'=>$is_existed[0]->id, 'user_id'=>$user_id))->get('rs_post')->result();
			if(empty($r)){
				//insert into rs_post
				$this->db->insert('rs_post',array('connection_id'=>$is_existed[0]->id,'user_id'=>$user_id,'posted_time'=>time()));
				//increase 1 at rs_connection
				$this->db->where('id',$is_existed[0]->id)->update('rs_connection',array('weight'=>$is_existed[0]->weight +1,'last_posted_time'=>time(),'is_processed'=>0 ));
			}
		}else{
			//insert into rs_connection
			$this->db->insert('rs_connection',
			array('upper_content_id'=>$high,'lower_content_id'=>$low,'weight'=>1,'is_processed'=>0,'last_posted_time'=>time()));
			//insert into rs_post
			$connection_id= $this->db->insert_id();	
			$this->db->insert('rs_post',array('connection_id'=>$connection_id,'user_id'=>$user_id,'posted_time'=>time()));
		}
	*/
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}	